package com.juxiao.xchat.base.utils;

import java.util.*;

public class BeautyNumberGenerator1 {
    private static final Map<Integer, String[]> LEVEL_PATTERNS = new HashMap<>();

    static {
        LEVEL_PATTERNS.put(30, new String[]{"######"}); // 6位数ID
        LEVEL_PATTERNS.put(40, new String[]{"#####"}); // 5位数ID
        LEVEL_PATTERNS.put(50, new String[]{"ABCDB", "ABCBA", "ABCAB", "AABBCC", "ABAAA", "AAAABC"});
        LEVEL_PATTERNS.put(60, new String[]{"AABCB", "ABCBBB", "AABCC", "AABBB", "AAAABB", "AAABBB"});
        LEVEL_PATTERNS.put(70, new String[]{"ABBBC", "ABBCC", "AAABC", "ABCCC", "AABBC", "AAAAAB", "ABBBBB"});
        LEVEL_PATTERNS.put(80, new String[]{"AAAAB", "ABAAA", "AABAA", "AAABA", "AAABB", "AABBB", "ABBBB", "ABBBA"});
        LEVEL_PATTERNS.put(100, new String[]{"####"}); // 4位数ID
    }

    public static List<String> generateBeautyNumbers(int level, String prefix, int count, Set<String> usedNumbers) {
        List<String> result = new ArrayList<>();
        Random random = new Random();

        // 限制 prefix 长度，避免极端情况导致死循环
        if (prefix.length() > 6) {
            return result;
        }

        // 限制 count，避免死循环
        if (prefix.length() >= 5) {
            count = Math.min(count, usedNumbers.size() + 10);
        }

        // 选择符合 level 的 pattern
        String[] patterns;
        if (level >= 100) {
            patterns = new String[]{"####"}; // 100级只能用 4 位数 ID
        } else {
            patterns = LEVEL_PATTERNS.entrySet().stream()
                    .filter(entry -> level >= entry.getKey())
                    .map(Map.Entry::getValue)
                    .findFirst()
                    .orElse(new String[0]);
        }

        if (patterns.length == 0) return result;

        int maxAttempts = 5000; // 限制最大尝试次数，避免死循环
        int attempt = 0;

        while (result.size() < count && attempt < maxAttempts) {
            String pattern = patterns[random.nextInt(patterns.length)];

            // 确保 pattern 长度 >= prefix
            if (pattern.length() < prefix.length()) continue;

            String candidate = generateNumber(pattern, prefix, random);

            if (candidate != null && !usedNumbers.contains(candidate)) {
                result.add(candidate);
                usedNumbers.add(candidate);
            }

            attempt++;  // 每次尝试+1
        }

        return result;  // 如果达到 maxAttempts，直接返回已有结果
    }

    private static String generateNumber(String pattern, String prefix, Random random) {
        // 如果前缀比模式长，直接返回空
        if (prefix.length() > pattern.length()) {
            return null;
        }

        Map<Character, Integer> mapping = new HashMap<>();
        String generated;
        int attempts = 1000; // 限制最大尝试次数，防止死循环

        do {
            StringBuilder sb = new StringBuilder(prefix); // 先填充 prefix
            mapping.clear();

            // 计算剩余长度
            int remainingLength = pattern.length() - prefix.length();
            if (remainingLength < 0) return null; // 避免 prefix 太长

            for (int i = 0; i < remainingLength; i++) {
                char c = pattern.charAt(i + prefix.length()); // 从 prefix 之后开始
                if (c == '#') {
                    sb.append(random.nextInt(10));
                } else {
                    mapping.putIfAbsent(c, random.nextInt(10));
                    sb.append(mapping.get(c));
                }
            }

            generated = sb.toString();

            if (--attempts == 0) {
                return null; // 达到尝试上限，返回空，避免死循环
            }
        } while (!generated.startsWith(prefix));

        return generated;
    }

    public static void main(String[] args) {
        Set<String> usedNumbers = new HashSet<>();
        usedNumbers.add("123456");
        usedNumbers.add("123452");

        List<String> numbers = generateBeautyNumbers(40, "10001", 24, usedNumbers);
        System.out.println(numbers);
    }
}
